import { Button, Form, Input, message } from 'antd'
import { UserOutlined, LockOutlined } from '@ant-design/icons';
import "../css/login.css";
import { useHistory } from 'react-router-dom';
import request from '../utils/requests';
import axios from 'axios';

export default function Login() {
    
    const history = useHistory();
    async function GoLogin(form :any) {
        ///这里是发请求和存token
        // login(form.name, form.password).then(response => {
        //     const {code,msg,data} = response.data;
        //     if(code === 200) {
        //         message.success(msg);
        //         set('token',data)
        //     } else {
        //         message.error(msg)
        //     }
        // })
        let data = {name : form.name, password: form.password}
        console.log(data);
        // var res = await request({
        //     url:"/api/login",
        //     methods: "POST",
        //     data: data
        // })
        // console.log(res);
        
        axios.post("/api/login",data).then((data) => {
            if(data.status == 200) {
                message.success("登录成功！")
                history.push('/admin/dashboard')
            } else {
                message.error("账号或者密码错误！")
            }
        })
    }
    return (
        <div>
            <div id ="login">
                <Form id = "login_form" 
                    initialValues={{ remember: true }}
                    onFinish={GoLogin}>
                    <Form.Item name="name" rules = {[
                        {
                            type: 'string',//类型
                            required: true//必填
                        }
                    ]}>
                        <Input prefix={<UserOutlined className="site-form-item-icon" />} placeholder="请输入你的账号" />

                    </Form.Item>
                    <Form.Item name="password" rules = {[
                        {
                            type : 'string',
                            required: true
                        }
                    ]}>
                        <Input.Password type="password" prefix={<LockOutlined className="site-form-item-icon" />} placeholder="请输入你的密码" />

                    </Form.Item>
                        <Form.Item >
                            账号：admin   密码：admin
                        </Form.Item>
                    <Form.Item>
                        <Button type="primary" htmlType="submit" className="login-form-button" >
                            登录
                        </Button>
                    </Form.Item>
                </Form>
            </div>
        </div>
    )
}